package com.ufm.security.action;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ufm.security.pojo.Roles;
import com.ufm.security.service.IRolesService;
import com.ufm.syslog.pojo.Syslog;
import com.ufm.syslog.service.ISyslogService;
import com.ufm.user.pojo.User;
import com.ufm.user.service.IUserService;
import com.ufm.util.PageUtil;
import com.ufm.util.PinyinUtil;

@Controller
@RequestMapping("/roles")
public class RolesController {
	@Autowired  
    private IRolesService rolesService;
	
	@Autowired
	private ISyslogService syslogService;
	
	@Autowired
	private IUserService userService;

	
	
/*	@RequestMapping(value="/addRoles")
	public String addRoles(Roles roles1, HttpServletRequest request) {
		//User userSession = (User)request.getSession().getAttribute("user");
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User userSession = userService.findByName(userDetails.getUsername());
		
		String appstu = request.getParameter("appstu");
		
		Roles roles = roles1;
		roles.setRecord_status(1);
		roles.setCreate_date(new Date());
		roles.setCreate_by(userSession.getUserName());
		
		rolesService.insert(roles);
		
		addLog(request, "角色维护", "增加角色:"+roles.getRole_name());
		
		return "redirect:/roles/findAll";
	}*/
	
	@RequestMapping(value="/deleteAll")
	public String deleteAll(@RequestParam("selIds") String selIds, HttpServletRequest request) {
		String[] ids = selIds.split(",");
		rolesService.deleteMore(ids,request);
		
		return "redirect:/roles/findAll";
	}
	
	@RequestMapping(value="/findById")
	public @ResponseBody Roles findById(Model model,HttpServletRequest request){
		String id = request.getParameter("id");
		Roles roles = rolesService.findById(Integer.parseInt(id));
		return roles;
	}
	
	@RequestMapping(value="/updateRoles")
	public String updateRoles(HttpServletRequest request) {
		//User userSession = (User)request.getSession().getAttribute("user");
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User userSession = userService.findByName(userDetails.getUsername());
		String id = request.getParameter("id");
		String role_name = request.getParameter("role_name");
		String role_desc = request.getParameter("role_desc");
		
		
		Roles roles = rolesService.findById(Integer.parseInt(id));
		roles.setRole_name(role_name);
		roles.setRole_desc(role_desc);
		
		rolesService.update(roles);
		
		addLog(request, "目录角色", "修改角色:"+role_name);
		return "redirect:/roles/findAll";
	}
	
	public void addLog(HttpServletRequest request, String moduleName, String action){
		//User userSession = (User)request.getSession().getAttribute("user");
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User userSession = userService.findByName(userDetails.getUsername());
		Syslog syslog = new Syslog(moduleName, action, 1, userSession.getUserId(), userSession.getUserName(), request.getRemoteAddr(), new Date());
		syslogService.insert(syslog);
	}
	
	@RequestMapping(value="/validateUsedForDelete")
	public @ResponseBody int validateUsedForDelete(@RequestParam("selIds") String selIds,HttpServletRequest request){
		String[] ids = selIds.split(",");
		int res = 0;
		for(int i = 0;i<ids.length;i++){
			res = rolesService.countUserUsed(Integer.parseInt(ids[i]));
			if(res>0){
				break;
			}
		}
		
		return res;
	}
	
	@RequestMapping(value="/validateUsed")
	public @ResponseBody int validateUsed(Model model,HttpServletRequest request){
		int tmid = Integer.parseInt(request.getParameter("tmid"));
		int res = 0;
		//res = catalogService.isParent(tmid);
		return res;
	}
}